package cn.tedu.travelsystem.base.utils;

import javax.servlet.http.HttpServletRequest;

public class IpUtil {

    public static String getRealIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多个IP时取第一个（最前面的是最真实的）
            if (ip.contains(",")) {
                ip = ip.split(",")[0];
            }
            return ip;
        }

        ip = request.getHeader("Proxy-Client-IP");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }

        ip = request.getHeader("WL-Proxy-Client-IP");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }

        // fallback 到 getRemoteAddr（最后的兜底）
        return request.getRemoteAddr();
    }
}
